package com.exp.controller;
import com.exp.dto.DeptPageQueryDTO;
import com.exp.pojo.DeptInf;
import com.exp.result.PageResult;
import com.exp.result.Result;
import com.exp.result.StatusCode;
import com.exp.service.DeptInfService;
import com.exp.utils.Message;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理模块-why
 */
@RestController
@RequestMapping("/admin/deptInf")
@Slf4j
public class DeptInfController {
    @Autowired
    private DeptInfService deptInfService;

    /*
     * 部门查询-分页查询
     * 搜索条件：部门名称
     */
//    @CrossOrigin(origins = "http://172.20.10.6:5173")
    @PostMapping("/page")
    public Result<PageResult> page(@RequestBody DeptPageQueryDTO depPageQueryDTO){
        PageResult pageResult = deptInfService.pageQuery(depPageQueryDTO);
        return Result.success(StatusCode.SUCCESS, Message.PAGE_SUCCESS,pageResult);
    }

    /*
     * 添加部门
     * 输入：部门名称、详细描述
     */
//    @CrossOrigin(origins = "http://172.20.10.6:5173")
    @PostMapping("/add")
    public Result<DeptInf> save(@RequestBody DeptInf deptInf){
        log.info("新增部门：{}",deptInf);
        deptInfService.save(deptInf);
        return Result.success(StatusCode.SUCCESS,Message.SAVE_SUCCESS,deptInf);
    }

    /*
     * 修改部门
     * 输入：部门名称、详细描述
     */
//    @CrossOrigin(origins = "http://172.20.10.6:5173")
    @PutMapping("/update")
    public Result update(@RequestBody DeptInf deptInf){
        log.info("编辑部门信息：{}", deptInf);
        deptInfService.update(deptInf);
        return Result.success(StatusCode.SUCCESS,Message.UPDATE_SUCCESS,deptInf);
    }

    /*
     * 批量删除用
     * 输入：id 集合
     */
    @DeleteMapping("/delete")
    public Result delete(@RequestParam List<Integer> ids) {
        log.info("批量删除部门信息：{}", ids);
        deptInfService.deleteBatch(ids);
        return Result.success(StatusCode.SUCCESS,Message.DELETE_SUCCESS,ids);
    }
}
